<template>
    <miliu-autograph v-model="isCanvas" @complete="complete" @back="back"></miliu-autograph>
</template>

<script setup>
import { ref, reactive, computed } from "vue";
import { navigateBack } from '@/utils/routerSkip.js';
import { useStore } from "vuex";
import { TEST_URL,PROD_URL, IMG_BASE_URL} from '@/constant/config.js'
import { TOKEN} from '@/constant/index.js'
import { getDataDictionaryFn, base64toFile } from '@/utils/index.js'
import {auditAssetCardCheckStock_PDA} from '@/api/api.js'
import miliuAutograph from "@/components/miliu-autograph/miliu-autograph";
import { OPERATION_COMMIT, OPERATION_SCAN, OPERATION_LOOK, USERINFO } from '@/constant/index.js'
//JS国际化引入
import { useI18n } from 'vue-i18n'

const { t } = useI18n()
components: {
    miliuAutograph
}
// 接受来自父组件的传参 页面传值
const props = defineProps({
    id: String,
    billCode : String
})
// 打开、关闭 签名组件页面
let isCanvas = ref(true)
let store = useStore();

// 确认事件
const complete = e => {
    // 返回本地生成的base图片路径  存到 store中
    store.commit('addFormData', e)
    // console.log(props.id)
    uploadFileMethod()
}
// 签名页面取消
const back = () => {
    uni.navigateBack({
        delta: 1
    });
}
const data = reactive({
    formData: '',
    billCode:props.billCode,
    id: props.id,// 上级界面传过来的id
	userInfo: uni.getStorageSync(USERINFO) || {},
})
// 签名信息
data.formData = computed(() => {
    return store.state.fromData
})
const uploadFileMethod = () => {
    let isDev = process.env.NODE_ENV === 'development'
    let baseUrl = isDev ? TEST_URL : PROD_URL                  
    let token = uni.getStorageSync(TOKEN)

    let formData = {
        id: data.id,
        type: 'wms_assets_h'
    }
    uni.showLoading({
        title: t('JsInternational.uploading')
    })
    // 调用uni 上传图片方法
    uni.uploadFile({
        url: baseUrl + '/api2/eletile-digital/sys-minio/upload', //上传接口
        name: 'file',
        header: {
           'Authorization':'Bearer '+ token
        },
        filePath: data.formData,
        formData: formData,
        success: (uploadFileRes) => {
            uni.hideLoading()
            const back = uploadFileRes
            try {
                let inputParameter = JSON.stringify(formData)
                let returnParameter = JSON.stringify(uploadFileRes)
                let operationPicture = '固资盘点'
                let callInterface = baseUrl + '/api2/eletile-digital/sys-minio/upload'
                let opertionParmes = {
                    inputParameter,
                    returnParameter,
                    operationPicture,
                    callInterface,
                    responseCode: uploadFileRes.statusCode,//返回状态
                    errorMessage: back.msg,//错误信息
                    isPass: back.statusCode == 200 ? 'Y' : 'N',//是否成功 Y N
                    operationType: '图片上传',
                    module: 'WMS',
                    source: "PDA",
                }
                basPdaOperationLogFn(opertionParmes)
            } catch (error) {
            }
            if (back.statusCode == 200) {
                let imgPath =back.data;
                checkRequest(imgPath)
            } else {
                uni.showToast({ title: back.message, icon: "none" });
            }
        },
        fail: () => {
            uni.hideLoading()
            uni.showToast(t('JsInternational.pictureUploading'))
            try {
                let inputParameter = '无'
                let returnParameter = '无'
                let operationPicture = '固资盘点'
                let callInterface = baseUrl + '/api2/eletile-digital/sys-minio/upload'
                let opertionParmes = {
                    inputParameter,
                    returnParameter,
                    operationPicture,
                    callInterface,
                    responseCode: '无',//返回状态
                    errorMessage: '上传失败',//错误信息
                    isPass: 'N',//是否成功 Y N
                    operationType: '图片上传',
                    module: 'WMS',
                    source: "PDA",
                }
                basPdaOperationLogFn(opertionParmes)
            } catch (error) {
            }
        },
        complete: function () {
            uni.hideLoading()
        }
    })
}

// 生成退料单
const checkRequest = (imgPath) => {
    let parmes = {
        detailId: data.id, // 收料单id
        auditSign: imgPath,
		billCode: data.billCode,
		CheckUserName: data.userInfo.name,
		CheckUserNo: data.userInfo.username,
    }
    console.log(parmes)
    auditAssetCardCheckStock_PDA(parmes).then(res => {
        if (res.code == 0) {
            uni.showToast({
                title: t('smashRemoval.submitSuccessfully'),
                icon: 'none',
                complete: () => {
                    //清除 缓存总store签名信息
                    store.commit('delFormDataAssets')
                    uni.navigateBack({
                        delta: 2
                    });
                }
            })

        } else {
            uni.showToast({ title: res.msg, icon: 'none' })
        }
    })
}
</script>

<style lang="scss">
.setclass {
    width: 100%;
    height: 800rpx;
}
</style>
